﻿var SmsCode = {
    model: function (d = {}) {
        this.Title = d.Title || "短信验证码";
        this.EnTitle = d.EnTitle || "";
        this.Tips = d.Tips || "";
        this.Name = d.Name || 'lg' + lg.GetSN(6, 10);
        this.Value = d.Value || "";
        this.Api = d.Api || "";
        this.Receiver = d.Receiver || "Phone";
        this.Width = d.Width || "100%";
        this.Required = true;
        this.IsRender = d.IsRender != undefined ? d.IsRender : true;
    },
    component: {
        name: "lg-form-smscode",
        data: {
            props: {
                items: {
                    type: Array,
                    default: []
                },
                value: {
                    default: ""
                },
                data: {
                    type: Object,
                    default: {}
                },
                requestParam: {
                    type: Object,
                    default: null
                },
                //0填写 1查看
                renderType: {
                    type: Number,
                    default: 0
                },
                eventHandler: {
                    type: Function,
                    default: null
                }
            },
            data() {
                return {
                    btText: "获取验证码",
                    disable: false
                }
            },
            methods: {
                getSmsCode: function () {
                    try {
                        if (this.disable) {
                            return false;
                        }
                        if (this.data.Receiver) {
                            var arr = this.items.filter(p => p.Data.Name == this.data.Receiver);
                            if (arr.length > 0) {
                                var type = arr[0].Data;
                                var res = type.verify();
                                if (res.state) {
                                    type.error = false;
                                    type.errMsg = '';
                                    this.disable = true;
                                    var that = this;
                                    var i = 60;
                                    var sendData = { phone: type.Value };
                                    //如果指定参数则使用指定参数，并将手机号码添加至对像
                                    if (this.requestParam != null) {
                                        sendData = this.requestParam;
                                        sendData.phone = type.Value
                                    }
                                    if (this.data.Api) {
                                        lg.request({
                                            url: this.data.Api,
                                            method: "post",
                                            data: sendData,
                                            success: res => {
                                                if (res.code == 0) {
                                                    layer.msg("短信已发送！", { icon: 1 });
                                                    var timer = setInterval(function () {
                                                        if (i > 0) {
                                                            i--;
                                                            that.btText = "获取验证码(" + i + ")"
                                                        } else {
                                                            that.disable = false;
                                                            that.btText = "获取验证码";
                                                            clearInterval(timer);
                                                        }
                                                    }, 1000)
                                                } else {
                                                    layer.msg(res.msg, { icon: 2 });
                                                    that.disable = false;
                                                }
                                            },
                                            fail: res => {
                                                layer.msg("发送失败！", { icon: 2 });
                                                that.disable = false;
                                            }
                                        })
                                    } else {
                                        layer.msg("缺少短信接口API!", { icon: 2 });
                                        return false;
                                    }

                                }
                                else {
                                    layer.msg(res.msg, { icon: 2 })
                                    type.error = true;
                                    type.errMsg = res.msg;

                                }
                            }
                            else {
                                layer.msg("指定的手机号字段不存在！", { icon: 2 })
                            }
                        }
                        else {
                            layer.msg("未指定接收验证码的手机号！", { icon: 2 })
                        }
                    } catch (e) {
                        layer.msg("执行异常，错误码：101！", { icon: 2 })
                    }

                }
            },
            template: `
			<div :class="['lg-form-item lg-form-smscode',{'lg-ver-error':data.error},{'pointer-none':renderType==1}]" :style="{'width':data.Width}" v-if="renderType==0">
              <div class="lg-form-block">
			      <div class="lg-form-title">
			         <div class="lg-form-title-item">
                        <i v-if="data.Required">*</i>
			            <span class="cn">{{data.Title}}</span>
                        <span class="en">{{data.EnTitle}}</span>
			         </div>			    
			      </div>
			      <div class="lg-form-control lg-form-flex">
			          <div class="lg-inline">
						 <input class="lg-input lg-input-text" :value="value" @input="$emit('update:value', $event.target.value)">
					  </div>
					  <div class="lg-inline">
						 <span :class="['lg-btn code-btn',{'disable':disable}]" v-on:click="getSmsCode">{{btText}}</span>
					  </div>
			      </div>
              </div>
              <div class="lg-form-block">
                  <div class="lg-form-tip"><p>{{data.Tips}}</p></div>
			      <div class="lg-form-ver" v-if="data.error">{{data.errMsg}}</div>
              </div>
			</div>
                      `
        }
    }
}

SmsCode.model.prototype.verify = function () {
    var res = { state: true, msg: "ok" };
    res.state = lg.regx.required(this.Value);
    if (!res.state) {
        res.msg = "验证码不可为空!";
        return res;
    }
    return res;
}
SmsCode.model.prototype.setValue = function () {
    this.Value = "";
}

export { SmsCode }